package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Pathogeny;
import com.itheima.service.PathogenyService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ${小帅帅}
 * @date 2019-11-03
 * 病因知识库
 */
@RestController
@RequestMapping("/pathogeny")
public class PathogenyController {

    @Reference
    private PathogenyService pathogenyService;

    //病因知识库文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {

        try {

            //使用POI的工具类,读取并解析文件，
            List<String[]> list = POIUtils.readExcel(excelFile);

            //创建一个集合，存储处理当前的list结构
            List<Pathogeny> pathogenyList = new ArrayList<>();

            //处理list，变成有利于数据库存储的格式
            for (String[] strings : list) {

                //获取LoreID，表格中，LoreID的索引为0
                String loreID1 = strings[0];
                long loreID = Integer.parseInt(loreID1);
                //获取标题。1
                String title=strings[1];
                //获取Keyword病因。2
                String keyword=strings[2];
                //获取LoreType病因类型。3
                String loreType1=strings[3];
                long loreType = Integer.parseInt(loreType1);
                //获取病情内容loreContent。4
                String loreContent=strings[4];
                //获取是否删除IsDelete.5
                String isDelete1=strings[5];
                long isDelete = Integer.parseInt(isDelete1);
                //获取备注Remark。6
                String remark=strings[6];
                //获取ChronicDisease是否是慢性疾病.7
                String ChronicDisease1 =strings[7];
                long chronicDisease = Integer.parseInt(ChronicDisease1);

                Pathogeny pathogenydata = new Pathogeny(loreID, title, keyword, loreType, loreContent, isDelete, remark, chronicDisease);
                //保存到list集合
                pathogenyList.add(pathogenydata);
            }

            //调用导入病因知识库的方法
            pathogenyService.pathogenyAdd(pathogenyList);


            //文件解析成功，给出提示信息
            return new Result(true, MessageConstant.IMPORT_P__SUCCESS);

        } catch (Exception e) {

            e.printStackTrace();
            //文件解析失败，给出提示信息
            return new Result(false, MessageConstant.IMPORT_P__FAIL);
        }
    }

    //病因知识库的分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) throws Exception {

        //病因知识库分页查询的调用
        PageResult page = pathogenyService.findPage(pageBean);
        return page;
    }

}
